angular.module('app', ['ionic']).config(function ($stateProvider, $urlRouterProvider) {
  $stateProvider.state("login", {
    url: "/login",
    templateUrl: "template/login.html"
  });
  $urlRouterProvider.otherwise('/login');
}).controller("LoginCtrl", function ($scope, $window, $http, $ionicLoading) {

  $scope.userName = getCache("userId");
  $scope.userPassword = getCache("userpassword");

  $scope.commitLogin = function (name, password) {

    if (name == null || name == undefined || name == "") {
      mui.alert("账号不能为空");
      return;
    }
    if (password == null || password == undefined || password == "") {
      mui.alert("密码不能为空");
      return;
    }
    var spinner = myUtil.startLoading("body");
    
    $http({
      url: API.LOGIN,
      params: {
        userId: name,
        password: password,
      },
      scope: $scope,
      method: 'POST',
    }).success(function (data) {

      if (data.status == "success" && data.code == 0) {
        saveCache("token", data.accountInfo.token);
        saveCache("userId", data.accountInfo.userId);
        saveCache("id", data.accountInfo.id);
        saveCache("username", data.accountInfo.username);
        saveCache("userpassword", password);

        var alias = data.accountInfo.id + name;
        //绑定jpush
        navigator.intent.JpushPlugin(alias);

        window.location.href = "./index.html";
        
        myUtil.stopLoading(spinner);
      } else {
        myUtil.stopLoading(spinner);
       
        mui.alert(data.msg, "提示");
      }
    }).error(function (data) {
      myUtil.stopLoading(spinner);
      $("#BgDiv").css("display", "none");
      mui.alert("请检查您的网络", "提示");
    });

  };

  //测试打印接口
  $scope.print = function () {
    navigator.print.print('', function (errorInfo) {
      alert(errorInfo);
    });
  };

  //忘记密码 
  $scope.forget = function () {
    mui.alert("请联系管理员", '提示');
  };
  $scope.register = function () {
    //跳转到注册账号页面
    mui.openWindow({
      url: "template/register.html"
    });
  }



});